package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AttributeMapper;
import cn.tedu.csmall.product.pojo.dto.*;
import cn.tedu.csmall.product.pojo.entity.Attribute;
import cn.tedu.csmall.product.pojo.vo.AttributeListVo;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVo;
import cn.tedu.csmall.product.service.AttributeService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AttributeServiceImpl implements AttributeService {

    @Autowired
    AttributeMapper mapper;

    @Override
    public void addNew(AttributeNewDto attributeNewDto) {
        int i = mapper.countByName(attributeNewDto.getName());
        if (i > 0){
            String message = "该名称已存在";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeNewDto,attribute);
        int insert = mapper.insert(attribute);
        if (insert != 1){
            String message = "添加失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
    }

    @Override
    public void delete(Long id) {
        AttributeStandardVo attributeStandardVo = mapper.selectById(id);
        if (attributeStandardVo == null) {
            String message = "该属性不存在";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        int i = mapper.deleteById(id);
        if (i != 1){
            String message = "删除不成功";
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
    }

    @Override
    public void updateInfoById(Long id, AttributeUpdateDto albumUpdateDTO) {
        AttributeStandardVo attributeStandardVo = mapper.selectById(id);
        if (attributeStandardVo == null) {
            String message = "该属性不存在";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        int i = mapper.countByName(albumUpdateDTO.getName());
        if (i > 0){
            String message = "名称已存在";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(albumUpdateDTO,attribute);
        attribute.setId(id);
        int insert = mapper.update(attribute);
        if (insert != 1){
            String message = "修改失败";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public List<AttributeListVo> list() {
        List<AttributeListVo> list = mapper.list();
        return list;
    }

    @Override
    public AttributeStandardVo getStandardById(Long id) {
        AttributeStandardVo attributeStandardVo = mapper.selectById(id);
        return attributeStandardVo;
    }
}
